//
//  YFMapSearchVC.m
//  project
//
//  Created by yefei on 2019/4/30.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMapSearchVC.h"
#import "YFMapSearchView.h"
#import "YFSearchView.h"

@interface YFMapSearchVC ()

@property (nonatomic,strong) UIButton *searchButton;

@property(nonatomic, strong) YFSearchView *searchView;

@end

@implementation YFMapSearchVC


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [YFMapSearchView.shared onceLocating];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSearchButton];
    [self.view addSubview:YFMapSearchView.shared];
}

- (void)setupSearchButton {
    UIButton *searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    searchButton.frame = CGRectMake(0, 0, (SCREEN_WIDTH - 72), 30);
    [searchButton setBackgroundColor:[UIColor withRGB:242]];
    [searchButton rounded:(UIRectCornerAllCorners) radius:5];
    [searchButton setTitle:@"正在定位..." forState:(UIControlStateNormal)];
    searchButton.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:16];
    [searchButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [searchButton setTitleEdgeInsets:(UIEdgeInsetsMake(0, 15, 0, 0))];
    self.navigationItem.titleView = searchButton;
    self.searchButton = searchButton;
    
    @weakify(self)
    [[searchButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.searchView showOn:self.navigationController.view];
    }];
    
    YFMapSearchView.shared.locatingSuccessed = ^(NSString * _Nonnull poiName) {
        @strongify(self)
        [self.searchButton setTitle:poiName forState:(UIControlStateNormal)];
    };
    
    YFMapSearchView.shared.completedAction = ^(YFMapSearchModel *model) {
        @strongify(self)
        if (self.searchCompleted) {
            self.searchCompleted(model);
        }
        [self.navigationController popViewControllerAnimated:YES];
    };
}


- (YFSearchView *)searchView {
    _searchView = YFSearchView.searchView;
    @weakify(self)
    _searchView.resultAction = ^(AMapPOI * _Nonnull poi) {
        @strongify(self)
        [self.searchButton setTitle:poi.name forState:(UIControlStateNormal)];
        YFMapSearchView.shared.poi = poi;
    };
    return _searchView;
}

@end
